﻿using System.Collections.Generic;
using System.Web.Mvc;
using YuFeng.Web.ViewModels;

namespace YuFeng.Web.Extentions
{
    public static class ControllerAlertExtentions
    {

        #region alert
        public static void Success(this Controller controller, string message, bool dismissable = false)
        {
            AddAlert(controller, AlertStyles.Success, message, dismissable);
        }

        public static void Information(this Controller controller, string message, bool dismissable = false)
        {
            AddAlert(controller, AlertStyles.Information, message, dismissable);
        }

        public static void Warning(this Controller controller, string message, bool dismissable = false)
        {
            AddAlert(controller, AlertStyles.Warning, message, dismissable);
        }

        public static void Danger(this Controller controller, string message, bool dismissable = false)
        {
            AddAlert(controller, AlertStyles.Danger, message, dismissable);
        }

        private static void AddAlert(Controller controller, string alertStyle, string message, bool dismissable)
        {

            var alerts = controller.TempData.ContainsKey(Alert.TempDataKey)
                ? (List<Alert>)controller.TempData[Alert.TempDataKey]
                : new List<Alert>();

            alerts.Add(new Alert
            {
                AlertStyle = alertStyle,
                Message = message,
                Dismissable = dismissable
            });

            controller.TempData[Alert.TempDataKey] = alerts;
        }
        #endregion
    }
}
